package com.DHH.Controller.Api;

import com.DHH.Config.ApiConstant;
import com.DHH.Config.Constant;
import com.DHH.Dto.ApiBikeMessage;
import com.DHH.Dto.Message;
import com.DHH.Entity.Base.BaseUser;
import com.DHH.Mapper.Api.ApiBaseUserMapper;
import com.DHH.Util.SMS;
import com.aliyuncs.exceptions.ClientException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Random;

/**
 * Created by 汪玉东 on 2017/8/5.
 */

@Controller
@RequestMapping("/api/sms/")
public class SMSController {
    @Resource
    private ApiBaseUserMapper apiBaseUserMapper;
    @RequestMapping("sendCode")
    @ResponseBody
    public ApiBikeMessage sendCodeByPhone(String mobile, HttpSession session){
        String code=String.valueOf((new Random().nextInt(8999) + 1000));
        ApiBikeMessage message= new ApiBikeMessage();
        try {

            if( SMS.send(mobile,code)){
                System.out.println(code);
                session.setAttribute(ApiConstant.WX_SMS_CODE_KEY,code);
                message.setStatus(0);
                message.setMessage("发送验证码成功");
                Object smsCode=session.getAttribute(ApiConstant.WX_SMS_CODE_KEY);
                System.out.println(smsCode);
            }else{
                message.setStatus(1);
                message.setMessage("发送验证码失败");
            }
        } catch (ClientException e) {
            //message.addError(e.getErrCode(),e.getErrMsg());
            message.setStatus(2);
            message.setMessage("发送验证码失败");
        }
        return message;
    }

}
